 #!/usr/bin/python



import logging
my_logger = logging.getLogger("xul_createProperty")
my_logger.setLevel(logging.DEBUG)

from constants import *

from followed_thread import FollowedThread



PROP_NAME = document.getElementById("propName")
INVPROP_BT = document.getElementById("btInvProp")
INVPROP_NAME_LABEL = document.getElementById("invPropNameLabel")
INVPROP_NAME = document.getElementById("inversePropName")
SUPER_PROPS = document.getElementById("superProperties")
DEFINE_TYPES = document.getElementById("defineTypes")
DOMAIN = document.getElementById("defDomain")
RANGE = document.getElementById("defRange")
HB_DOMAIN_RANGE = document.getElementById("hbDomainRange")
TYPES_PROGRESS = document.getElementById("progressTypes")


THREAD_TYPES = "ThreadTypes"
document.thread_types = None

document.types = None



############################################################

def onLoad():
	
	my_logger.debug("onLoad()")
	
	from the_nepomuk_whisperer import TheNepomukWhisperer
	document.NEPO = TheNepomukWhisperer()
	
	SUPER_PROPS.builder.refresh()
	PROP_NAME.focus()
	SUPER_PROPS.selectedIndex = 0
	
############################################################

def genInvPropName():
	
	INVPROP_NAME.value = "inverse of " + PROP_NAME.value
	
############################################################

def superPropertiesClick():
	
	superPropUri = ""
	if SUPER_PROPS.selectedIndex != -1:
		superPropUri = SUPER_PROPS.selectedItem.childNodes.item(1).getAttribute("label")
	
	if superPropUri == PIMO_DESCRIBINGPROPERTY:
		INVPROP_BT.setAttribute("hidden", "true")
		INVPROP_NAME_LABEL.setAttribute("hidden", "true")
		INVPROP_NAME.setAttribute("hidden", "true")
		DEFINE_TYPES.checked = False
		defineTypes()
	else:
		INVPROP_BT.setAttribute("hidden", "false")
		INVPROP_NAME_LABEL.setAttribute("hidden", "false")
		INVPROP_NAME.setAttribute("hidden", "false")
		
############################################################

def defineTypes():
	
	my_logger.debug("defineTypes()")
	
	if document.thread_types != None:
		document.thread_types.kill()
	
	if DEFINE_TYPES.checked == False:
		
		requestStopped()
		my_logger.debug("DEFINE_TYPES.checked = False")
		HB_DOMAIN_RANGE.setAttribute("hidden", "true")
		
	else: # The user wants to define domain & range types.
		
		requestRun()
		
		my_logger.debug("DEFINE_TYPES.checked = True")
		HB_DOMAIN_RANGE.setAttribute("hidden", "false")
		
		# clear lists
		while DOMAIN.childNodes.length != 0:
			DOMAIN.removeChild(DOMAIN.firstChild)
		while RANGE.childNodes.length != 0:
			RANGE.removeChild(RANGE.firstChild)
		
		document.thread_types = FollowedThread(window, THREAD_TYPES, getTypes, (), addTypesToList)
		document.thread_types.run()

############################################################

def getTypes():
	
	document.types = document.NEPO.interface.getAllTypes()

############################################################

def addTypesToList():
	
	# naming & lists filling
	for my_type in document.types:
		name = document.NEPO.pimo.getName(my_type)
		logger.debug("name = %s" % name)
		logger.debug("my_type = %s" % my_type)
		DOMAIN.appendItem(name, my_type)
		RANGE.appendItem(name, my_type)
	
	requestStopped()

############################################################

def requestRun():
	
	TYPES_PROGRESS.hidden = False

############################################################

def requestStopped():
	
	TYPES_PROGRESS.hidden = True

############################################################

def wizardFinish():
	
	my_logger.debug("wizardFinish()!")
	
	prop_name = PROP_NAME.value
	inversePropName = INVPROP_NAME.value
	superPropUri = ""
	if SUPER_PROPS.selectedIndex != -1:
		superPropUri = SUPER_PROPS.selectedItem.childNodes.item(1).getAttribute("label")
	
	if prop_name == "":
		PROMPTS.alert(window, "Missing information", "Please enter a name for this new property.")
		return 1
	
	if (inversePropName == "") and (superPropUri != PIMO_DESCRIBINGPROPERTY):
		PROMPTS.alert(window, "Missing information", "Please enter a name for the inverse of this new property.")
		return 1
	
	if superPropUri == "":
		PROMPTS.alert(window, "Missing information", "You must a select a super property from the list.")
		return 1
	
	try:
		property_uri = None
		if superPropUri == PIMO_DESCRIBINGPROPERTY:
			# Register this new property as a describing property. ("object-to-value")
			property_uri = document.NEPO.pimo.createDescribingProperty(USERNAME, prop_name)
		else:
			# Register this new property as an "object-to-object" property.
			if DEFINE_TYPES.checked == False: # no defined types
				property_uri = document.NEPO.pimo.createProperty(USERNAME, prop_name, superPropUri, inversePropName)
			else: # user has defined types
				if DOMAIN.selectedIndex == -1 or RANGE.selectedIndex == -1:
					PROMPTS.alert(window, "Missing information", "Please select the wanted domain and range types, or uncheck the \"define types\" checkbox if you do not want to.")
					return 1
				else:
					property_uri = document.NEPO.pimo.createProperty(USERNAME, prop_name, superPropUri, inversePropName, DOMAIN.selectedItem.value, RANGE.selectedItem.value)
		
		# See if the property has properly been created
		if property_uri == None:
			PROMPTS.alert(window, "Error", "An unknown error has occurred in property creation.")
			window.close()
		else:
			PROMPTS.alert(window, "Information", "The property named \"" + prop_name + "\" has properly been created.")
			window.close()
		
	except CanNotException, e:
		PROMPTS.alert(window, "Error", str(e))

############################################################

def onUnload():
	
	logger.debug("onUnload()")
	if document.thread_types != None:
		document.thread_types.kill()









